package org.adorsys.aderp.orga.service;

import java.util.List;

import org.adorsys.aderp.orga.domain.OrgaPerson;
import org.adorsys.aderp.orga.domain.OrgaRelation;
import org.adorsys.aderp.orga.domain.OrgaRelationRole;
import org.adorsys.aderp.orga.domain.OrgaResource;
import org.adorsys.aderp.orga.domain.ResResRelation;
import org.adorsys.aderp.orga.domain.ResourceRelationRole;
import org.adorsys.aderp.orga.repository.ResResRelationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;


public class ResResRelationServiceImpl implements ResResRelationService {
	
	@Autowired
    ResResRelationRepository resResRelationRepository;
    
	
	public List<ResResRelation> findByOrgaResource(OrgaResource orgaResource){
		return resResRelationRepository.findByOrgaResource(orgaResource);
	}
	public List<ResResRelation> findByTargetResource(OrgaResource orgaResource){
		return resResRelationRepository.findByTargetResource(orgaResource);
	}

	public Page<ResResRelation> findByResourcePage(OrgaResource orgaResource,Integer page, Integer size){
		return resResRelationRepository.findByOrgaResource(orgaResource, new PageRequest(page, size));
	}
	public Page<ResResRelation> findByTargetResourcePage(OrgaResource orgaResource,Integer page, Integer size){
		return resResRelationRepository.findByTargetResource(orgaResource,new PageRequest(page, size));
	}
	
	public	Page<ResResRelation> searchResResRelation(OrgaResource orgaResource, OrgaResource targetResource,
			ResourceRelationRole relationRole,Integer page, Integer size){
		PageRequest pageable = new PageRequest(page, size);
        if(orgaResource!=null && targetResource==null && relationRole==null){
        	return resResRelationRepository.findByOrgaResource(orgaResource, pageable);
        } else if (orgaResource!=null && targetResource!=null && relationRole==null){
        	return resResRelationRepository.findByOrgaResourceAndTargetResource(orgaResource, targetResource, pageable);
        } else if (orgaResource!=null && targetResource!=null && relationRole!=null){
        	return resResRelationRepository.findByOrgaResourceAndTargetResourceAndRelationRole(orgaResource, targetResource, relationRole, pageable);
        } else if (orgaResource==null && targetResource!=null && relationRole==null){
        	return resResRelationRepository.findByTargetResource(orgaResource, pageable);
        } else if (orgaResource==null && targetResource!=null && relationRole!=null){
        	return resResRelationRepository.findByTargetResourceAndRelationRole(targetResource, relationRole, pageable);
        } else if (orgaResource==null && orgaResource==null && relationRole!=null){
        	return resResRelationRepository.findByRelationRole(relationRole, pageable);
        } else {
        	return resResRelationRepository.findAll(pageable);
        }
	}
}
